/**
 * This program is free software but WITHOUT ANY WARRANTY OR CONDITIONS OF ANY KIND.
 *
 * Copyright (C) 2008-2013 www.pretty-tools.com
 */

/**
 * Manages dialog and other windows of web slide show
 */
var wsshWindowManager = {
    logger: yrprxFactory.createLogger("wsshWindowManager"),
    globalDialogKeeper: null,
    theLastSlideShowDialog: null,
    theLastSlideShowDialogListeners: []
};

wsshWindowManager.openSlideShow = function(images, title, forceUseTheLastWindow)
{
    var manager = this;
    try
    {
        if (!this.globalDialogKeeper)
        {
            this.globalDialogKeeper = function(port) {
                if (port.name == "clearTheLastSlideShowDialog")
                    port.onMessage.addListener(function(msg)
                    {
                        if (msg.clearTheLastSlideShowDialog && manager.theLastSlideShowDialog
                                && msg.clearTheLastSlideShowDialog.id == manager.theLastSlideShowDialog.id) {
                            manager.theLastSlideShowDialog = null;
                        }
                    });
            };

            chrome.runtime.onConnect.addListener(this.globalDialogKeeper);
        }

        if (wsshConfig.singleSlideShowDialog && this.theLastSlideShowDialog || forceUseTheLastWindow)
        {
            setTimeout(function () {
                chrome.tabs.sendMessage(manager.theLastSlideShowDialog.tabs[0].id, {additionalImages: images});
                chrome.windows.update(manager.theLastSlideShowDialog.id, {focused: true});
            }, 500);
            return;
        }
    }
    catch(ex)
    {
        if (!this.logger)
            this.logger = yrprxFactory.createLogger("wsshWindowManager");

        if (this.logger.enabled) this.logger.error("constructor: ", ex);
    }

    var args = [images];
    if (title) args[1] = title;

    chrome.windows.create({url: chrome.extension.getURL("ui/SlideShow.htm"), left: 0, top: 0, width: screen.width, height: screen.height, focused: true, type: "popup"}, function(wnd){
        setTimeout(function () {
            chrome.tabs.sendMessage(wnd.tabs[0].id, {arguments: args, thisWnd: wnd});
        }, 500);
        manager.theLastSlideShowDialog = wnd;

        if (manager.theLastSlideShowDialogListeners.length > 0)
        {
            for (var i in manager.theLastSlideShowDialogListeners)
                manager.theLastSlideShowDialogListeners[i](wnd);
            manager.theLastSlideShowDialogListeners = [];
        }
    });
}

wsshWindowManager.openImportRules = function(rulesURL)
{
    var args = [rulesURL];
    chrome.windows.create({url: chrome.extension.getURL("ui/ImportRules.htm"), width: 700, height: 600, focused: true, type: "popup"}, function(wnd){
        setTimeout(function () {
            chrome.tabs.sendMessage(wnd.tabs[0].id, {arguments: args});
        }, 500);
    });
}

wsshWindowManager.openOptions = function()
{
    window.open(chrome.extension.getURL("ui/Options.htm"), "WebSlideShowOptions");
}

wsshWindowManager.openRule = function(param)
{
    return window.showModalDialog(chrome.extension.getURL("ui/Rule.htm"), [param, chrome.i18n.getMessage, wsshResources], "dialogWidth=700px;dialogHeight=250px;status=no;center=yes;resizable=no;");
}

wsshWindowManager.newXMLHttpRequest = function()
{
    return new XMLHttpRequest();
}
